<script>
addLoadEvent(function() {
	$.validator.addMethod("validateEvalCost",function(value,element,params){
		var yearArray = $("input[datarule='year']",'.evalcosttr');
		var index = $(element).attr('index');
		var value = $(element).val();
		for(var i=0;i<yearArray.length;i++) {
			var evalCost = yearArray[i];
			if($(evalCost).attr("index")!=index && $(evalCost).val()==value && value!='') {
				return false;
			}
		}
		return true;
	},'年度不能重复！');
	$.validator.addMethod("validateCompleteCost",function(value,element,params){
		var year = document.getElementById('${field.name}completeCostYear');
		var cost = document.getElementById('${field.name}completeCost');
		if((year.value!='' && cost.value=='') || (year.value=='' && cost.value!='')) {
			return false;
		}
		return true;
	},'年度与年底前完成投资必须同时填写！');
	$("input[datarule='year']",'.evalcosttr').each(function(){
		var optionRule = {};
		optionRule["validateEvalCost"] = true;				
		$(this).rules("add", optionRule);
	});
	$("input[datarule='year']",'.completeCost').each(function(){
		var optionRule = {};
		optionRule["validateCompleteCost"] = true;			
		$(this).rules("add", optionRule);
	});
	validatorForm();
});
var evalCostIndex = <#if evalcost_list?? && evalcost_list?size gt 0>${evalcost_list?size+1}<#else>1</#if>;
<#if completeCost??>
	evalCostIndex++;
</#if>
function addEvalCost() {
	var length = $('.evalcosttr').not('.completeCost').length;
	if(length>5) {
		alert('最多只允许填写5年的投资费用！');
		return false;
	}
	evalCostIndex++;
	var tr = document.createElement('tr');
	tr.id='${field.name}tr'+evalCostIndex;
	$(tr).attr('class','listviewTR evalcosttr');
	var html = '';
	html += "<td class=\"listviewTD\" align=\"right\"><font color=\"red\">*</font>年度:</td>";
	html += "<td class=\"listviewContent\" align=\"left\">";
	html += "<input type=\"text\" datarule=\"year\" index=\""+evalCostIndex+"\" name=\"fieldMap['${field.name}'].evalCostList["+evalCostIndex+"].investYear\"  datarulejson='{\"min\":\"1978\",\"displayName\":\"年度\",\"dataType\":\"year\",\"isRequired\":\"1\",\"length\":\"4\"}' style=\"width:1309x;\" class=\"Wdate\" onfocus=\"WdatePicker({dateFmt:'yyyy'});\">";
	html += "</td>";
	html += "<td class=\"listviewTD\" align=\"right\"><font color=\"red\">*</font>投资费用:</td>";
	html += "<td class=\"listviewContent\" align=\"left\">";
	html += "<input type=\"text\" datarule=\"number\" name=\"fieldMap['${field.name}'].evalCostList["+evalCostIndex+"].investCost\" datarulejson='{\"displayName\":\"投资费用\",\"dataType\":\"number\",\"precision\":\"6\",\"isRequired\":\"1\",\"length\":\"8\"}' style=\"width:130px;\" >";
	html += "&nbsp;&nbsp;万元";
	html += "</td>";
  html += "<td class=\"listviewContent\" colspan=\"2\" align=\"left\">";
  html += "<div style=\"float:left;margin-right:15px;\"><a href=\"javascript:void(0);\" onclick=\"removeEvalCost('"+evalCostIndex+"');return false;\">删除</a><div>";
  html += "</td>";
	//html += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";


	$(tr).html(html);
	var trArray = $('.evalcosttr').not('.completeCost');
	$(tr).insertBefore($(trArray[trArray.length-1]));
	$("input[datarule='year']",$(tr)).each(function(){
		var optionRule = {};
		optionRule["validateEvalCost"] = true;				
		$(this).rules("add", optionRule);
	});
	validatorForm();
}
function removeEvalCost(index) {
	var number = 0;
	$("input",document.getElementById('${field.name}tr'+index)).each(function(){
		var name = $(this).attr('name');
		$(".error").each(function(){
			var forName = $(this).attr('for');
			if(name==forName) {
				$(this).parent().hide();
				number++;
			}
		})
	});
	var errorNumber = $("li",".errorDIv").length;
	if(number == errorNumber) {
		$("ol",".errorDIv").hide();
		$(".container").hide();
	}
	$(document.getElementById('${field.name}tr'+index)).remove();
	validatorForm();
	
}

</script>
<#if formField.hidden?? && formField.hidden=="1">
	<input type=hidden name="fieldMap['${field.name}'].id"  <#if businessInstanceField?? && businessInstanceField.id??>value="${businessInstanceField.id}"</#if>>
	<input type=hidden name="fieldMap['${field.name}'].field.id"  value="${formField.field.id}">
	<input type=hidden id=${field.name} name="fieldMap['${field.name}'].value" <#if businessInstanceField??>value="${businessInstanceField.value}"</#if>></input>
<#else>
	<input type=hidden name="fieldMap['${field.name}'].id"  <#if businessInstanceField?? && businessInstanceField.id??>value="${businessInstanceField.id}"</#if>>
	<input type=hidden name="fieldMap['${field.name}'].field.id"  value="${formField.field.id}">
	<input type=hidden name="${field.name}" id="valid${field.name}" datarule="string"  datarulejson='{"displayName":"${field.displayName}","dataType":"string","isRequired":"0"}' <#if evalcost_list?? && evalcost_list?size gt 0>value="${evalcost_list?size}"</#if>>
	
	<#if showCompleteInvest??>
	<#if !formField.readonly?? || formField.readonly=="0">
		<tr class="listviewTR evalcosttr completeCost">
			<td class="listviewTD" align="right">年度:</td>
			<td class="listviewContent" align="left">
			<input type="text" datarule="year" id="${field.name}completeCostYear" index="0" name="fieldMap['${field.name}'].evalCostList[0].investYear"  datarulejson='{"min":"1978","displayName":"年度","dataType":"year","isRequired":"0","length":"4"}' class="Wdate" onfocus="WdatePicker({dateFmt:'yyyy'});" <#if completeCost??>value="${completeCost.investYear?default('')}"</#if> style="width:130px;">
			</td>
			<td class="listviewTD" align="right">年底前完成投资:</td>
			<td class="listviewContent" colspan="3" align="left">
				<input type="text" datarule="number" id="${field.name}completeCost" name="fieldMap['${field.name}'].evalCostList[0].investCost" datarulejson='{"displayName":"年底前完成投资","dataType":"number","precision":"6","isRequired":"0","length":"8"}' <#if completeCost?? && completeCost.investCost??>value="${completeCost.investCost?c}"</#if> style="width:130px;" >&nbsp;&nbsp;万元
				<input type="hidden" name="fieldMap['${field.name}'].evalCostList[0].isCompleteCost" value="1"/> 
			</td>
		</tr>
	<#else>
		<#if completeCost??>
			<tr class="listviewTR evalcosttr completeCost">
			<td class="listviewTD" align="right">年度:</td>
			<td class="listviewContent" align="left">
				${completeCost.investYear?default('')}
			</td>
			<td class="listviewTD" align="right">年底前完成投资:</td>
			<td class="listviewContent" colspan="3" align="left">
				<#if completeCost.investCost??>${completeCost.investCost?c}</#if>&nbsp;&nbsp;万元
			</td>
		</tr>
		</#if>
	</#if>
	</#if>
	
	<#if evalcost_list?? && evalcost_list?size gt 0>
		<#assign index=1>
		<#list evalcost_list as cost>
			<tr class="listviewTR evalcosttr" id="${field.name}tr${index}">
				<input type="hidden" name="fieldMap['${field.name}'].evalCostList[${index}].id" value="${cost.id}">
				<td class="listviewTD" align="right"><font color="red">*</font>年度:</td>
				<td class="listviewContent" align="left">
				<#if !formField.readonly?? || formField.readonly=="0">
				<input type="text" datarule="year" index="${index}" name="fieldMap['${field.name}'].evalCostList[${index}].investYear"  datarulejson='{"min":"1978","displayName":"年度","dataType":"year","isRequired":"1","length":"4"}' class="Wdate" onfocus="WdatePicker({dateFmt:'yyyy'});" style="width:130px;" value="${cost.investYear?default('')}">
				<#else>
				${cost.investYear?default('')}
				</#if>
				</td>
				<td class="listviewTD" align="right"><font color="red">*</font>投资费用:</td>
				<td class="listviewContent" align="left">
				<#if !formField.readonly?? || formField.readonly=="0">
				<input type="text" datarule="number" name="fieldMap['${field.name}'].evalCostList[${index}].investCost" datarulejson='{"displayName":"投资费用","dataType":"number","precision":"6","isRequired":"1","length":"8"}' style="width:130px;" <#if cost.investCost??>value="${cost.investCost?c}"</#if>>
				<#else>
				<#if cost.investCost??>${cost.investCost?c}</#if>
				</#if>
				&nbsp;&nbsp;万元
				</td>
        <td class="listviewContent" colspan="2" align="left">
            <#if !formField.readonly?? || formField.readonly=="0">
                <div style="float:left;margin-right:15px;"><a href="javascript:void(0);" onclick="removeEvalCost('${index}');return false;">删除</a></div>
            <#else>
								&nbsp;&nbsp;
            </#if>
				</td>
                
			</tr>
		<#assign index=index+1>
		</#list>
	</#if>
	
	<#if !formField.readonly?? || formField.readonly=="0">
	<tr class="listviewTR evalcosttr">
		<td class="listviewTD" align="left">&nbsp;</td>
		<td class="listviewContent" align="left" colspan="5"><a href="#" class="button" tabindex="-1"><input type="button" onclick="addEvalCost();return false;" value="添加"></a></td>
	</tr>
	</#if>
</#if>
